package repositories

import (
	models "devops-dcc/internal/model"
	"errors"
	"fmt"
	"gorm.io/gorm"
)

type PlatformRepository interface {
	CreatePlatform(platform *models.Platform) error
	GetPlatformByID(id uint) (*models.Platform, error)
	GetAllPlatforms() ([]models.Platform, error)
	UpdatePlatform(platform *models.Platform) error
	DeletePlatform(id uint) error

	CreateEnvironment(env *models.Environment) error
	GetEnvironmentByID(id uint) (*models.Environment, error)
	GetAllEnvironments() ([]models.Environment, error)

	SetPlatformEnvConfig(platformID, envID uint, config string) error
	GetPlatformEnvConfig(platformID, envID uint) (*models.PlatformEnvironment, error)
}

type GormPlatformRepository struct {
	db *gorm.DB
}

func NewGormPlatformRepository(db *gorm.DB) *GormPlatformRepository {
	return &GormPlatformRepository{db: db}
}

// 平台CRUD
func (r *GormPlatformRepository) CreatePlatform(platform *models.Platform) error {
	return r.db.Create(platform).Error
}

func (r *GormPlatformRepository) GetPlatformByID(id uint) (*models.Platform, error) {
	var platform models.Platform
	if err := r.db.First(&platform, id).Error; err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return nil, errors.New("platform not found")
		}
		return nil, err
	}
	return &platform, nil
}

func (r *GormPlatformRepository) GetAllPlatforms() ([]models.Platform, error) {
	var platforms []models.Platform
	if err := r.db.Find(&platforms).Error; err != nil {
		return nil, err
	}
	return platforms, nil
}

func (r *GormPlatformRepository) UpdatePlatform(platform *models.Platform) error {
	return r.db.Save(platform).Error
}

func (r *GormPlatformRepository) DeletePlatform(id uint) error {
	return r.db.Transaction(func(tx *gorm.DB) error {
		// 先删除关联的环境配置
		if err := tx.Where("platform_id = ?", id).Delete(&models.PlatformEnvironment{}).Error; err != nil {
			return err
		}
		// 再删除平台
		return tx.Delete(&models.Platform{}, id).Error
	})
}

// 环境CRUD
func (r *GormPlatformRepository) CreateEnvironment(env *models.Environment) error {
	fmt.Println("env", env)
	return r.db.Create(env).Error
}

func (r *GormPlatformRepository) GetEnvironmentByID(id uint) (*models.Environment, error) {
	var env models.Environment
	if err := r.db.First(&env, id).Error; err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return nil, errors.New("environment not found")
		}
		return nil, err
	}
	return &env, nil
}

func (r *GormPlatformRepository) GetAllEnvironments() ([]models.Environment, error) {
	var envs []models.Environment
	if err := r.db.Find(&envs).Error; err != nil {
		return nil, err
	}
	return envs, nil
}

// 平台环境配置
func (r *GormPlatformRepository) SetPlatformEnvConfig(platformID, envID uint, config string) error {
	return r.db.
		Where(models.PlatformEnvironment{
			PlatformID:    platformID,
			EnvironmentID: envID,
		}).
		Assign(models.PlatformEnvironment{Config: config}).
		FirstOrCreate(&models.PlatformEnvironment{}).
		Error
}

func (r *GormPlatformRepository) GetPlatformEnvConfig(platformID, envID uint) (*models.PlatformEnvironment, error) {
	var config models.PlatformEnvironment
	err := r.db.
		Preload("Platform").
		Preload("Environment").
		Where("platform_id = ? AND environment_id = ?", platformID, envID).
		First(&config).Error

	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return nil, errors.New("config not found")
		}
		return nil, err
	}
	return &config, nil
}
